package com.mall.server.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.autoconfig.exception.BusinessException;
import com.mall.dubbo.api.SmsHomeRecommendSubjectApi;
import com.mall.model.domain.SmsHomeRecommendSubject;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SmsHomeRecommendSubjectService {

    @DubboReference
    private SmsHomeRecommendSubjectApi smsHomeRecommendSubjectApi;

    //分页查询推荐
    public CommonPage list(Integer pageNum, Integer pageSize, SmsHomeRecommendSubject subject) {

        String subjectName = subject.getSubjectName();
        Integer recommendStatus = subject.getRecommendStatus();

        Page<SmsHomeRecommendSubject> page = smsHomeRecommendSubjectApi.listPage(pageNum, pageSize, subjectName, recommendStatus);

        return new CommonPage(page);

    }

    //添加首页推荐专题
    public void create(List<SmsHomeRecommendSubject> homeRecommendSubjectList) {

        for (SmsHomeRecommendSubject subject : homeRecommendSubjectList) {
            Random r = new Random();
            int sort = r.nextInt(100) + 50;
            subject.setSort(sort);
            subject.setRecommendStatus(1);
        }

        smsHomeRecommendSubjectApi.saveBatch(homeRecommendSubjectList);

    }

    //批量删除推荐
    public void delete(List<Long> ids) {

        smsHomeRecommendSubjectApi.removeByIds(ids);
    }

    //批量修改推荐状态
    public void update(List<Long> ids, SmsHomeRecommendSubject subject) {

        List<SmsHomeRecommendSubject> list = smsHomeRecommendSubjectApi.findByIds(ids, subject);

        if (!CollUtil.isEmpty(list)) {
            throw new BusinessException("这是错误的操作");
        }
        smsHomeRecommendSubjectApi.updateByIds(ids, subject);

    }

    //修改推荐排序
    public void updateSort(Integer id, SmsHomeRecommendSubject subject) {

        smsHomeRecommendSubjectApi.updateSort(id, subject);


    }

}
